const db = wx.cloud.database()        //初始化数据库环境
const users = db.collection('users')  //设置相应的云数据库集合
var app = getApp()
Component({
  options: {
    addGlobalClass: true
  },
  /**
 * 组件的初始数据
 */
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    userInfo: {},
    hasUserInfo: false,
    manage:'',
    password:'',
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  pageLifetimes: {
    show:function(){
      if (app.globalData.userInfo) {
        console.log(app.globalData.userInfo)
        this.setData({
          userInfo: app.globalData.userInfo,
          hasUserInfo: true
        })
      }
      else if(this.data.canIUse) {
       
        wx.getUserInfo({
          success: res => {
            app.globalData.userInfo = res.userInfo
            console.log(app.globalData.userInfo)
            this.setData({
              userInfo: res.userInfo,
              hasUserInfo: true
            })
          }
        })
      } else {
        // 在没有 open-type=getUserInfo 版本的兼容处理
        wx.getUserInfo({
          success: res => {
            app.globalData.userInfo = res.userInfo
            this.setData({
              userInfo: res.userInfo,
              hasUserInfo: true
            })
          }
        })
      }
    },
  },
  methods: {
    getUserInfo: function (e) {
      console.log(e)
      wx.setStorage({
        key: 'userInfo',
        data: e.detail.userInfo,
      })
      app.globalData.userInfo = e.detail.userInfo      //将用户信息保存到全局变量
      this.setData({
        userInfo: e.detail.userInfo,
        hasUserInfo: true
      })
    },
    showQrcode() {
      wx.previewImage({
        urls: ['cloud://scxsh-003-n7w1s.7363-scxsh-003-n7w1s-1301280263/zanshang.jpg'],  
      })
    },
    contact() {
      wx.previewImage({
        urls: ['https://ae01.alicdn.com/kf/H1cf7444467f4435d960a6feab2954c51L.jpg'],
       })
    },
    showModal(e) {
      this.setData({
        modalName: e.currentTarget.dataset.target
      })
    },
    hideModal(e) {
      this.setData({
        modalName: null
      })
    },
    //接收管理员账户名
    manage: function (e) {
      console.log(e.detail.value)
      this.setData({
        manage: e.detail.value,
      })
    },
    //接收管理员密码
    password: function (e) {
      console.log(e.detail.value)
      this.setData({
        password: e.detail.value,
      })
    },
    login: function () {
      var that = this;
      if (that.data.manage == '') {
        wx.showModal({
          title: '提示!',
          content: '请输入账号',
        })
      } else if (that.data.password == '') {
        wx.showModal({
          title: '提示!',
          content: '请输入密码',
        })
      } else {
                wx.showToast({
                  title: '登陆成功',
                })
                wx.navigateTo({
                  url: '/pages/my/manage/manage'
                })
              
       }
    }
  },
 

})

    